﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using AgilePlannerDT.Utils;
using System.Windows.Input;

namespace AgilePlannerDT.Display.CustomWidgets
{
    public class ResizerHandle : UserControl
    {
        #region Constants

        public static readonly double DEFAULT_RESIZER_HANDLE_SIZE =45;

        #endregion

        #region Resizer Handle

        protected Polygon resizerHandle = new Polygon();
        protected Point rsp1, rsp2, rsp3;
        protected List<Line> grips = new List<Line>();
        protected double handleSize;
        protected Canvas canvas;

        #endregion

        #region Constructor

        public ResizerHandle()
            : this(DEFAULT_RESIZER_HANDLE_SIZE)
        {
        }

        public ResizerHandle(double handleSize)
        {
            this.handleSize = handleSize;
            Width = handleSize;
            Height = handleSize;
            
            Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));

            canvas = new Canvas();
            canvas.HorizontalAlignment = HorizontalAlignment.Stretch;
            canvas.VerticalAlignment = VerticalAlignment.Stretch;
            canvas.Margin = new Thickness(0);
            AddChild(canvas);
            
            rsp1 = new Point(0, handleSize);
            rsp2 = new Point(handleSize, handleSize);
            rsp3 = new Point(handleSize, 0);

            resizerHandle.Points.Add(rsp1);
            resizerHandle.Points.Add(rsp2);
            resizerHandle.Points.Add(rsp3);

            resizerHandle.Stroke = Brushes.Black;
            resizerHandle.StrokeThickness = 1;
            resizerHandle.Fill = Brushes.LightGray;

            canvas.Children.Add(resizerHandle);

            Line grip;

            for (int i = 0; i < 4; i++)
            {
                grip = new Line();
                grip.X1 = rsp1.X + 15 + i*5 ;
                grip.X2 = rsp3.X - 5;
                grip.Y1 = rsp1.Y - 5;
                grip.Y2 = rsp3.Y + 15 + i * 5;
                grip.Stroke = Brushes.DarkGray;
                grips.Add(grip);
                canvas.Children.Add(grip);
            }
        }

        #endregion

        #region Members
       
        public Point[] GetCorners()
        {
            return new Point[3] { rsp1, rsp2, rsp3 };
        }

        public bool IsInsideResizerHandleArea(Point p)
        {
            return Util.IsPointInsidePolygon(p, GetCorners());
        }

        #endregion
    }
}
